package com.cdbkj.cloud.pos.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;

import com.androidyang.library.common.adapter.CommonAdapter;
import com.androidyang.library.common.adapter.Wrapper;
import com.androidyang.library.http.okhttp.bean.HttpResult;
import com.androidyang.library.http.okhttp.listener.HttpClientHandler;
import com.cdbkj.cloud.pos.R;
import com.cdbkj.cloud.pos.http.HttpRequest;
import com.cdbkj.cloud.pos.bean.JsonResult;
import com.cdbkj.cloud.pos.utils.Tools;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RegTypeFragment extends Fragment implements AdapterView.OnItemClickListener, HttpClientHandler {
    private static final String ARG_PARAM1 = "userId";

    private String userId;

    protected View rootView;
    protected GridView mGridView;
    private GridAdapter adapter;
    private List<Map> list;

    private OnRegTypeFragmentInteractionListener mListener;

    private View.OnClickListener clickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Tools.dismissDialog();
            Tools.backgroundAlpha(getActivity(), 1);
            mListener.onRegTypeJumpPreFragmentInteraction();
        }
    };

    public RegTypeFragment() {
        // Required empty public constructor
    }

    public static RegTypeFragment newInstance(String userId) {
        RegTypeFragment fragment = new RegTypeFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, userId);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            userId = getArguments().getString(ARG_PARAM1);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_reg_type, container, false);
        initView(rootView);
        return rootView;
    }

    private void initView(View rootView) {
        mGridView = (GridView) rootView.findViewById(R.id.regType_gridView);
        mGridView.setOnItemClickListener(this);
        //    HttpRequest.regType("regType",this,userId);
        adapter = new GridAdapter(getActivity(), null);
        mGridView.setAdapter(adapter);
        initParams();
    }

    public void initParams() {
        list=new ArrayList<>();
        Map ordinary = new HashMap();
        ordinary.put("typeId", "普通号,便民号");
        ordinary.put("typeName", "普通号");
        list.add(ordinary);

        Map expert= new HashMap();
        expert.put("typeId","主任号,副主任号");
        expert.put("typeName","专家号");
        list.add(expert);
        Map emergency = new HashMap();
        emergency.put("typeId","急诊号");
        emergency.put("typeName","急诊号");
        list.add(emergency);
        adapter.setList(list, true);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnRegTypeFragmentInteractionListener) {
            mListener = (OnRegTypeFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Map item = (Map) adapterView.getAdapter().getItem(i);
        if (item.get("typeId") == null) {
            Tools.showDialog("HIS数据错误，无挂号类别", getActivity());
            return;
        }
        if (item.get("typeName") == null) {
            Tools.showDialog("HIS数据错误，无挂号类别", getActivity());
            return;
        }
        mListener.onRegTypeFragmentInteraction(item.get("typeId").toString(), item.get("typeName").toString());
    }

    @Override
    public void httpClientSuccess(Object o, HttpResult httpResult) {
        JsonResult json = (JsonResult) httpResult.getResult();
        if (json == null) {
            Tools.showDialog("服务器处理异常", getActivity());
            return;
        }
        if (!json.isSuccess()) {
            Tools.showDialog(json.getMsg(), getActivity(), clickListener, clickListener);
            return;
        }
        list = (List) json.getData();
    }

    @Override
    public void httpClientError(Object o, HttpResult httpResult) {
        Tools.showDialog("网络异常", getActivity());
    }

    public interface OnRegTypeFragmentInteractionListener {
        void onRegTypeFragmentInteraction(String regTypeId, String regTypeName);

        void onRegTypeJumpPreFragmentInteraction();
    }

    class GridAdapter extends CommonAdapter<Map> {

        public GridAdapter(Context context, List<Map> list) {
            super(context, R.layout.item_reg_type, list);
        }

        @Override
        public void convert(Wrapper<Map> wrapper, Map item) {
            wrapper.setText(R.id.regTypeName_tv, (String) item.get("typeName"));
        }
    }
}
